package ru.jetsoft.reg112.desktop.data;

import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.*;

import java.math.BigInteger;

public class Docx4jTest {

    private static final String TEST_FILE = "D:/test.docx";

    private static ObjectFactory factory = Context.getWmlObjectFactory();
    private WordprocessingMLPackage wordMLPackage;
    private String fileName = null;

    public Docx4jTest(String fileName) {
        this.fileName = fileName;
        try {
            wordMLPackage = WordprocessingMLPackage.createPackage();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void save() throws Exception {
        wordMLPackage.save(new java.io.File(fileName));
    }

    public void add(Object obj) {
        wordMLPackage.getMainDocumentPart().addObject(obj);
    }

    public Tbl createTable() {
        return factory.createTbl();
    }

    public Tr createTableRow(Tc... tableCells) {
        Tr result = factory.createTr();
        for (Tc cell : tableCells) {
            result.getContent().add(cell);
        }
        return result;
    }

    public Tc createTableCell(P paragraph) {
        Tc result = factory.createTc();
        result.getContent().add(paragraph);
        return result;
    }

    public Tc[] createTableCell(String... text) {
        Tc[] result = new Tc[text.length];
        for (int i = 0; i < text.length; i++) {
            result[i] = createTableCell(createParagraphOfText(text[i]));
        }
        return result;
    }

    public Tc[] createTableCell(String style, String... text) {
        Tc[] result = new Tc[text.length];
        for (int i = 0; i < text.length; i++) {
            result[i] = createTableCell(createStyledParagraphOfText(style, text[i]));
        }
        return result;
    }

    public Tc createTableCell(String content) {
        return createTableCell(createParagraphOfText(content));
    }

    public Tc createTableCell(String style, String text) {
        return createTableCell(createStyledParagraphOfText(style, text));
    }

    public P createParagraphOfText(String text) {
        return wordMLPackage.getMainDocumentPart().createParagraphOfText(text);
    }

    public P createStyledParagraphOfText(String style, String text) {
        return wordMLPackage.getMainDocumentPart().createStyledParagraphOfText(style, text);
    }

    public void addRow(Tbl table, Tr... row) {
        for (Tr r : row) {
            table.getContent().add(r);
        }
    }

    private void addBorders(Tbl table) {
        table.setTblPr(new TblPr());
        CTBorder border = new CTBorder();
        border.setColor("auto");
        border.setSz(new BigInteger("4"));
        border.setSpace(new BigInteger("0"));
        border.setVal(STBorder.SINGLE);

        TblBorders borders = new TblBorders();
        borders.setBottom(border);
        borders.setLeft(border);
        borders.setRight(border);
        borders.setTop(border);
        borders.setInsideH(border);
        borders.setInsideV(border);
        table.getTblPr().setTblBorders(borders);
    }

    public static void main(String[] args) throws Exception {
        String styledTitle = "Styled Title";
        String styledSubtitle = "Styled Subtitle";

        String simpleText = "Text without style";

        String[] firstCells = {"First", "Second", "Third", "Forth"};
        String[] secondCells = {"F", "S", "T", "F"};

        Docx4jTest docx = new Docx4jTest(TEST_FILE);

        // Create styled paragraphs
        docx.add(docx.createStyledParagraphOfText("Title", styledTitle));
        docx.add(docx.createStyledParagraphOfText("Subtitle", styledSubtitle));
        docx.add(docx.createStyledParagraphOfText("heading 1", styledSubtitle));
        docx.add(docx.createStyledParagraphOfText("heading 2", styledSubtitle));
        docx.add(docx.createStyledParagraphOfText("heading 3", styledSubtitle));

        // Create simple paragraph
        docx.add(docx.createParagraphOfText(simpleText));

        // Create table with data
        Tbl table = docx.createTable();
        Tc[] first = docx.createTableCell("Subtitle", firstCells);
        Tc[] second = docx.createTableCell(secondCells);
        docx.addRow(table, docx.createTableRow(first));
        docx.addRow(table, docx.createTableRow(second));
        docx.addBorders(table);
        docx.add(table);

        docx.save();
    }
}
